//
//  SetUserPermanentAddrView.swift
//  IdleFish
//
//  Created by Ben on 2022/1/16.
//

import SwiftUI

struct SetUserPermanentAddrView: View {
    
    @EnvironmentObject var userDataVM: UserDataViewModel
    @Namespace var animation
    
    @StateObject var locationManager = LocationManager.shared
    
    init() {
        UITableView.appearance().showsVerticalScrollIndicator = false
    }
    
    var body: some View {
        VStack(spacing: 0) {
            // 这里添加一个右侧的按钮
            CustomNavView(content: {
                Text("选择常住地").font(.system(size: 16, weight: .semibold))
            }) {
                userDataVM.showPermanentAddr.toggle()
            }
            .overlay {
                
                HStack {
                    Button {
                        Dlog("保存常住地")
                        Task {
                            await userDataVM.updatePermanentAddr()
                        }
                    } label: {
                        
                        Text("保存")
                            .frame(width: 50, height: 30)
                            .font(.system(size: 12))
                            .foregroundColor(userDataVM.permanentAddrSaveBtnIsEnabled ? .black : Color(hex: 0xA8A7A8))
                            .background {
                                Capsule()
                                    .fill(userDataVM.permanentAddrSaveBtnIsEnabled  ? .yellow : Color(hex: 0xCECECD))
                            }
                        
                    }
                    // 根据属性值设置禁用状态
                    .disabled(!userDataVM.permanentAddrSaveBtnIsEnabled)
                }
                .frame(maxWidth: .infinity, alignment: .trailing)
                .padding(.trailing, 6)

            }
            
            HStack {
                VStack(alignment: .leading) {
                    Text("当前位置")
                        .font(.system(size: 12))
                        .foregroundColor(Color.black.opacity(0.4))
                    
                    HStack {
                        
                        ZStack {
                            if let cityName = locationManager.cityName, cityName.count > 0 {
                                Text(cityName)
                            } else {
                                ProgressView()
                            }
                        }
                        .frame(minWidth: 40)
                           
                       
                        
                        Image("location_line")
                            .resize(size: CGSize(width: 14, height: 14), contentModel: .fit)
                        
                        Spacer()
                        
                        Button {
                            let provinceModel = ProvinceModel()
                            provinceModel.name = locationManager.provinceName
                            userDataVM.selectedProvince = provinceModel
                            
                            let cityModel = CityModel()
                            cityModel.name = locationManager.cityName
                            userDataVM.selectedCity = cityModel
                            Task {
                                await userDataVM.updatePermanentAddr()
                            }
                        } label: {
                            Text("使用当前城市")
                                .font(.system(size: 16))
                        }
                        // 根据属性值设置禁用状态
                        .disabled((locationManager.cityName?.isEmpty ?? true))

                    }
                
                    Divider()
                        .foregroundColor(Color.black.opacity(0.4))
                        .frame(height: 1)
                }
                .padding([.top, .leading, .trailing], 10)
            }
            
            HStack {
                List(userDataVM.provinceList) { provinceModel in
                    
                    HStack(spacing: 10) {
                         
                        if userDataVM.selectedProvince == provinceModel {
                            Color.yellow
                                .frame(width: 5, height: 14)
                                .matchedGeometryEffect(id: "test", in: animation)
                        } else {
                            Color.white
                                .frame(width: 5, height: 14)
                        }
                        
                        
                        Text(provinceModel.name ?? "")
                            .font(.system(size: 14))
                            .foregroundColor(Color.black.opacity(userDataVM.selectedProvince == provinceModel ? 1 : 0.4))

                    }
                    .padding(.trailing, 5)
                    .contentShape(Rectangle())
                    .listRowInsets(.zero)
                    .listRowSeparator(.hidden)
                    .onTapGesture {
                        withAnimation {
                            userDataVM.selectedProvince = provinceModel
                        }
                    }
                        
                }
                .frame(width: 120)
                .listStyle(.plain)
                
                
                Divider()
                    .foregroundColor(Color.black.opacity(0.4))
                    .frame(width: 1)
                
                List(userDataVM.cityList) { cityModel in
                    HStack {
                        Text(cityModel.name ?? "")
                            .font(.system(size: 14))
                            .foregroundColor(Color.black.opacity(userDataVM.selectedCity == cityModel ? 1 : 0.4))
                    }
                    .contentShape(Rectangle())
                    .onTapGesture(perform: {
                        userDataVM.selectedCity = cityModel
                    })
                        .listRowInsets(.zero)
                        .listRowSeparator(.hidden)
                        
                }
                .listStyle(.plain)
            }
        }
        .navigationBarHidden(true)
        .navigationTitle("")
        .onAppear {
            Task {
                await userDataVM.loadCity()
            }
            
            locationManager.startUpdating()
        }
    }
}

struct SetUserPermanentAddrView_Previews: PreviewProvider {
    static var previews: some View {
        SetUserPermanentAddrView()
    }
}
